%
% This script is part of the project work for T-61.6040
% course 'Learning from Multiple Sources' organized in fall 2010.
%
% This script can be used as the basis for solving the multi-view
% learning part of the project.
%

%
% Read in the data
%
load('faces.mat');
data = cell(1,9);
for condition=1:9;
  data{condition} = [males{condition};females{condition}];

  % Subtract the mean value of each face to get rid of average
  % image brightness
  data{condition} = data{condition} - repmat(mean(data{condition},2),[1 256]);
end;

%
% Reduce the dimensionality of the data with PCA
%
for condition=1:9;
  [pcaproj,projected,val] = princomp(data{condition});
  data{condition} = projected(:,1:20); % Keep 20 dimensions
end;

% Use the male/female grouping to indicate the true clustering
% 1=male, 2=female
true = [ones(20,1);2*ones(20,1)];

% Try with different numbers of clusters
K_set = [2:2:10];

% Store the results for different variants
res_single = cell(1,length(K_set));
res_pooled = cell(1,length(K_set));
res_multi = cell(1,length(K_set));

% Go through a few different clustering complexities
ki = 0; % used for indexing the result arrays
for K=K_set;
  ki = ki + 1;

  display(sprintf('Clustering into K=%d clusters',K))

  % Go through the views and perform standard single-view K-means
  % clustering
  for condition=1:9;
    display(sprintf(' Running single-view condition %d',condition))

    % Find the K-means solution
    %%% TO BE DONE %%%
    assignment = floor(K*rand(40,1))+1; % Random assignment as a placeholder

    % Store the cost; assumes the final clusterassignment
    % is stored in the vector 'assignment'
    res_single{ki} = [res_single{ki};clusterMI(true,assignment)];
  end;


  % Go through all possible pairs of views and perform both pooled
  % and multi-view clustering
  for cond1=1:9;
    for cond2=(cond1+1):9;
      display(sprintf('  Running pooled conditions %d and %d',cond1,cond2))
      % Find the pooled K-means solution
      %%% TO BE DONE %%%
      assignment = floor(K*rand(40,1))+1; % Random assignment as a placeholder
      
      % Store the cost; assumes the final cluster assignment
      % is stored in the vector 'assignment'
      res_pooled{ki} = [res_pooled{ki};clusterMI(true,assignment)];

    
      display(sprintf('  Running multi-view conditions %d and %d',cond1,cond2))
      % Find the multi-view K-means solution
      %%% TO BE DONE %%%
      assignment = floor(K*rand(40,1))+1; % Random assignment as a placeholder
    
      % Store the cost; assumes the final clust assignment
      % is stored in the vector 'assignment'
      res_multi{ki} = [res_multi{ki};clusterMI(true,assignment)];
    end;
  end;
end;

%
% Analyze the results to find out how well the different variants
% performed. Also plot some figures for the report.
%
%%% TO BE DONE %%%
